"""
每日报表，发邮件
"""
import os
from datetime import datetime, timedelta, date
import logzero
import pymysql
from logzero import logger
from config import DEBUG
from send_email import get_email_html, sendmail
from utils import get_mongo_collection

pwd = os.path.dirname(os.path.abspath(__file__))
current_module = os.path.splitext(os.path.basename(__file__))[0]
logzero.logfile(f"/tmp/{current_module}.log", maxBytes=30_000_000, backupCount=3, encoding='utf-8')
today = date.today()
yesterday_end = datetime(today.year, today.month, today.day, 0, 0, 0)
yesterday_begin = yesterday_end - timedelta(days=1)
# today_10 = yesterday_end + timedelta(hours=10)
today_10 = datetime.now()

BL_MYSQL_CONF = {
    'host': '10.10.6.6',
    'port': 3306,
    'user': 'develop',
    'password': '123^%$-qwe',
    'db': 'lt',
    'charset': 'utf8mb4',
    # 'cursorclass': pymysql.cursors.DictCursor
}


def get_spider_count():
    collection_names = ['bcy_cos_detail', 'bcy_cos_list', 'zhihu_zhuanlan', 'lofter',
                        'bilibili_submitvideo_list', 'toutiao', 'diyidan', 'weibo_cos']
    report_dict = {}
    yesterday_sum = 0
    yesterday_used_sum = 0
    today_sum = 0
    for c in collection_names:
        db = get_mongo_collection(c)
        yesterday_count = db.find({'update_time': {'$gte': yesterday_begin, '$lte': yesterday_end}}).count()
        yesterday_used_count = db.find({'update_time': {'$gte': yesterday_begin, '$lte': yesterday_end},
                                        'used': {'$exists': True}}).count()
        today_10_count = db.find({'update_time': {'$gte': yesterday_end, '$lte': today_10}}).count()
        report_dict.setdefault(c, {'yesterday_count': yesterday_count, 'yesterday_used_count': yesterday_used_count,
                                   'today_10_count': today_10_count})
        logger.info((c, yesterday_count, yesterday_used_count, today_10_count))
        yesterday_sum += yesterday_count
        yesterday_used_sum += yesterday_used_count
        today_sum += today_10_count
    return today_sum, yesterday_used_sum, yesterday_sum, report_dict


def get_connection():
    connection = pymysql.connect(**BL_MYSQL_CONF)
    return connection


def get_contents_total_yesterday_and_today():
    sql = "SELECT count(*) as num FROM contents  where status=0;"
    connection = get_connection()
    with connection.cursor() as cursor:
        cursor.execute(sql)
        total_today = cursor.fetchone()[0]
        total_yesterday = store_today_record_and_get_yesterday_record(total_today)
    logger.info(f'total_yesterday={total_yesterday} , total_today={total_today}')
    return total_yesterday, total_today


def store_today_record_and_get_yesterday_record(total_today):
    # 构造今日的total并插入
    COLLECTION_NAME = 'contents_total'
    db = get_mongo_collection(COLLECTION_NAME)
    data = {'total': total_today, 'date': today.strftime('%Y%m%d')}
    db.insert(data)
    # 查询出昨天的total，如果有多条求最大值。如果没有为0
    data_yesterday_list = []
    for d in db.find({'date': yesterday_begin.strftime('%Y%m%d')}):
        data_yesterday_list.append(d['total'])
    total_yesterday = max(data_yesterday_list) if len(data_yesterday_list) > 0 else 0
    return total_yesterday


def main():
    today_sum, yesterday_used_sum, yesterday_sum, report_dict = get_spider_count()
    total_yesterday, total_today = get_contents_total_yesterday_and_today()
    mail = get_email_html(today_sum=today_sum, yesterday_used_sum=yesterday_used_sum, yesterday_sum=yesterday_sum,
                          report_dict=report_dict, total_yesterday=total_yesterday, total_today=total_today)
    env = '测试环境' if DEBUG else '生产环境'
    sendmail(mail, f'每日轻点通知({env})', [], '爬虫蜘蛛')


if __name__ == '__main__':
    main()
